package com.apes.fn.scm.saleReport.service.process;

import com.apes.fn.fico.expense.platformFee.PlatformFeeProcess;
import com.apes.framework.api.SpringManager;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 用与获取指定的处理器
 */
@Component
public class SaleReportFactory implements InitializingBean {

    private static List<SaleReportProcess> processList = null;

    @Override
    public void afterPropertiesSet() {
        if (processList == null) {
            processList = new ArrayList<>();
            Map<String, SaleReportProcess> beansOfType = SpringManager.getApplicationContext().getBeansOfType(SaleReportProcess.class);
            beansOfType.forEach((key, value) -> processList.add(value));
        }
    }

    public static SaleReportProcess get(String orderType) {
        SaleReportProcess result = null;
        for (SaleReportProcess process : processList) {
            if (process.support(orderType)) {
                result = process;
                break;
            }
        }
        if (Objects.isNull(result)) throw new RuntimeException(orderType + " 没有找到对应的处理器，请检查");
        return result;
    }

}
